package com.pr.makfilm.admin.pages.user.list.cmp;

import com.pr.makfilm.core.cmp.notify.Notification;
import com.pr.makfilm.core.cmp.web.BootstrapEnumDropDownPanel;
import com.pr.makfilm.core.cmp.web.BootstrapPasswordFieldPanel;
import com.pr.makfilm.core.cmp.web.BootstrapTextFieldPanel;
import com.pr.makfilm.core.cmp.window.AbstractWindow;
import com.pr.makfilm.domain.User;
import com.pr.makfilm.domain.UserTypeEnum;
import com.pr.makfilm.service.IUserService;
import org.apache.commons.lang3.StringUtils;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.model.Model;
import org.apache.wicket.model.PropertyModel;
import org.apache.wicket.spring.injection.annot.SpringBean;
import org.apache.wicket.validation.validator.EmailAddressValidator;

/**
 * Created with IntelliJ IDEA.
 * User: Pawel
 * Date: 09.09.13
 * Time: 22:21
 * To change this template use File | Settings | File Templates.
 */
public abstract class UserWindow extends AbstractWindow<User>
{
   @SpringBean
   private IUserService userService;

   private String confirmedPassword;

   private boolean createMode = true;

   public UserWindow(String id)
   {
      super(id, Model.of(new User()), new Model<String>());
   }

   @Override
   protected void onInitialize()
   {
      super.onInitialize();

      BootstrapTextFieldPanel firstName = new BootstrapTextFieldPanel("firstName", new PropertyModel(this, "user." + User.FIELD_FIRST_NAME));
      firstName.setValidation(true);
      form.add(firstName);

      BootstrapTextFieldPanel lastName = new BootstrapTextFieldPanel("lastName", new PropertyModel(this, "user." + User.FIELD_LAST_NAME));
      lastName.setValidation(true);
      form.add(lastName);

      BootstrapTextFieldPanel login = new BootstrapTextFieldPanel("login", new PropertyModel(this, "user." + User.FIELD_LOGIN));
      login.setValidation(true);
      form.add(login);

      BootstrapPasswordFieldPanel password = new BootstrapPasswordFieldPanel("password", new PropertyModel(this, "user." + User.FIELD_PASSWORD))
      {
         @Override
         protected void onConfigure()
         {
            setVisible(createMode);
         }
      };
      password.setValidation(true);
      form.add(password);

      BootstrapPasswordFieldPanel rePassword = new BootstrapPasswordFieldPanel("rePassword", new PropertyModel(this, "confirmedPassword"))
      {
         @Override
         protected void onConfigure()
         {
            setVisible(createMode);
         }
      };
      rePassword.setValidation(true);
      form.add(rePassword);

      BootstrapTextFieldPanel email = new BootstrapTextFieldPanel("email", new PropertyModel(this, "user." + User.FIELD_EMAIL));
      email.setValidation(true);
      email.addValidator(EmailAddressValidator.getInstance());
      form.add(email);

      BootstrapEnumDropDownPanel<UserTypeEnum> type = new BootstrapEnumDropDownPanel("type", new PropertyModel(this, "user." + User.FIELD_TYPE), UserTypeEnum.class);
      type.setValidation(true);
      form.add(type);
   }

   public void resetState(boolean createMode)
   {
      this.createMode = createMode;
      if (createMode)
      {
         setUser(new User());
         confirmedPassword = "";
      }
      setHeader(createMode ? getString("create.title") : getString("edit.title"));
   }

   @Override
   public void onSubmit(AjaxRequestTarget target, Form<?> form)
   {
      User user = getUser();
      if (createMode())
      {

         if (StringUtils.equals(user.getPassword(), getConfirmedPassword()))
         {
            userService.save(user);
            Notification.success(getString("user.successfully.saved"));
         }
         else
         {
            Notification.error("password.does.not.match");
         }
      }
      else
      {
         User old = userService.getBySid(user.getSid());
         old.setLogin(user.getLogin());
         old.setFirstName(user.getFirstName());
         old.setLastName(user.getLastName());
         old.setEmail(user.getEmail());
         old.setType(user.getType());
         userService.update(old);
         Notification.success(getString("user.successfully.updated"));
      }
   }

   public User getUser()
   {
      return (User) getDefaultModelObject();
   }

   public void setUser(User user)
   {
      setDefaultModelObject(user);
   }

   public String getConfirmedPassword()
   {
      return confirmedPassword;
   }

   public void setConfirmedPassword(String confirmedPassword)
   {
      this.confirmedPassword = confirmedPassword;
   }

   public boolean createMode()
   {
      return createMode;
   }
}
